using System;
using AutoMapper;
using SapaFinance.DTO;
using SapaFinance.Domain.Model;

namespace SapaFinance.OOMap.Helpers
{
    public class OrderToOrderDTOConvert : TypeConverter<Order, OrderDTO>
    {

        protected override OrderDTO ConvertCore(Order order)
        {
            var orderDto = new OrderDTO();
            if (order != null){
                orderDto.UID = order.UID;
                orderDto.Number=order.Unique.ToString();
                orderDto.CreatedBy=order.CreatedBy.User.FullName;
                orderDto.Created=order.CreatedBy.DateTime;
                orderDto.ChangedBy = order.ChangedBy.User.FullName;
                orderDto.Changed = order.ChangedBy.DateTime;
                if (order.ExecutedBy != null){
                    orderDto.ExecutedBy = order.ExecutedBy.User.FullName;
                    orderDto.Executed =order.ExecutedBy.DateTime;
                }
                orderDto.Payment=order.Payment.Amount;
                orderDto.PayeeSharePie=order.GetPayeeSharePie().Name;
                var payer = order.GetPayerSharePie();
                if (payer != null){
                    orderDto.PayerSharePie = order.GetPayerSharePie().Name;
                }
                orderDto.CurrencyName=order.Payment.Currency.Name;
                orderDto.SelectedAgent=order.Agent.UID;
                orderDto.SelectedCurrency=order.Payment.Currency.UID;
                orderDto.AgentName=order.Agent.Name;
                orderDto.PaymentPurpose = order.PaymentPurpose;
                orderDto.PaymentPeriod = order.PaymentPeriod;
            }
            return orderDto;
        }
    }
}